package com.linq.cool.sdk.core;

import com.linq.cool.sdk.enums.ResultEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @author: yqlin
 * @date: 2021/4/24 16:41
 * @description: 响应信息主体
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Data
public class R<T> implements Serializable {

    private static final long serialVersionUID = 6641063799012399957L;
    private int code;
    private String message;
    private T data;

    public R(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public static <T> R<T> ok() {
        return new R<>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMessage());
    }


    public static <T> R<T> ok(String message) {
        return new R<>(ResultEnum.SUCCESS.getCode(), message);
    }

    public static <T> R<T> ok(String message, T data) {
        return new R<>(ResultEnum.SUCCESS.getCode(), message, data);
    }

    public static <T> R<T> ok(T data) {
        return new R<>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMessage(), data);
    }

    public static <T> R<T> fail() {
        return new R<>(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMessage());
    }

    public static <T> R<T> fail(String message) {
        return new R<>(ResultEnum.FAIL.getCode(), message);
    }


    public static <T> R<T> fail(ResultEnum responseEnum) {
        return new R<>(responseEnum.getCode(), responseEnum.getMessage());
    }

    public static <T> R<T> fail(Integer code, String message) {
        return new R<>(code, message);
    }
}
